package com.example.springboot.mybatis.web;

import com.example.springboot.mybatis.entity.User;
import com.example.springboot.mybatis.service.UserService;
import com.example.springboot.mybatis.service.UserService2;
import com.example.springboot.mybatis.util.UUIDUtil;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @desc mybatis 操作数据库查询示例
 * @Author wangsh
 * @date 2018/5/5 23:18
 * @return
 */
@RestController
@RequestMapping("/user")
public class UserController {

	/**
	 * 通过注解方式查询数据库
	 */
	@Autowired
	private UserService userService;

	@RequestMapping("/getUserById")
	@ResponseBody
	public User getUserById(Integer id) {
		User User = userService.getUserById(id);
		return User;
	}

	@RequestMapping("/getUserByPage")
	@ResponseBody
	public List<User> getUserByPage(String name) {
		// 设置分页
		PageHelper.startPage(1, 2);
		List<User> users = userService.getUserByPage(name);
		return users;
	}

	@RequestMapping("/save")
	@ResponseBody
	public User save() {
		User user = new User();
		user.setId(UUIDUtil.getUUID());
		user.setUsername("lisi");
		user.setPassword("lisi");
		userService.saveUser(user);
		return user;
	}

	/**
	 * 通过mybatis xml配置文件读取数据
	 */
	@Autowired
	private UserService2 userService2;

	@RequestMapping("/queryList2")
	@ResponseBody
	public List<User> queryList() {
		List<User> queryList = userService2.queryList();
		return queryList;
	}

	@RequestMapping("/save2")
	@ResponseBody
	public User save2() {
		User user = new User();
		user.setId(UUIDUtil.getUUID());
		user.setUsername("lisi");
		user.setPassword("lisi");
		userService2.save(user);
		return user;
	}
}
